package com.chuangjiangx.member.score.ddd.query;

import com.chuangjiangx.member.basic.dao.mapper.InMbrConfigMapper;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGrandTotalRuleMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrConfig;
import com.chuangjiangx.member.basic.dao.model.InMbrConfigExample;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGrandTotalRule;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGrandTotalRuleExample;
import com.chuangjiangx.member.score.ddd.query.dto.MbrScoreNoticeDTO;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Auther: YZJ
 * @Date: 2018/9/4 14:46
 * @Description:
 */
@Component
public class MbrScoreNoticeQuery {
    @Autowired
    private InMbrScoreGrandTotalRuleMapper inMbrScoreGrandTotalRuleMapper;
    @Autowired
    private InMbrConfigMapper inMbrConfigMapper;

    /**
     * 查询商户获取积分须知
     */
    public MbrScoreNoticeDTO queryScoreNotice(@Param(value = "merchantId") MerchantId merchantId) {
        MbrScoreNoticeDTO mbrScoreNoticeDTO = new MbrScoreNoticeDTO();
        InMbrScoreGrandTotalRuleExample example = new InMbrScoreGrandTotalRuleExample();
        example.createCriteria().andMerchantIdEqualTo(merchantId.getId()).andEnableEqualTo(Byte.valueOf("1"));
        List<InMbrScoreGrandTotalRule> inMbrScoreGrandTotalRules = inMbrScoreGrandTotalRuleMapper.selectByExample(example);
        if (inMbrScoreGrandTotalRules != null && !inMbrScoreGrandTotalRules.isEmpty()) {
            BeanUtils.copyProperties(inMbrScoreGrandTotalRules.get(0),mbrScoreNoticeDTO);
        }
        InMbrConfigExample inMbrConfigExample = new InMbrConfigExample();
        inMbrConfigExample.createCriteria().andMerchantIdEqualTo(merchantId.getId());
        List<InMbrConfig> inMbrConfigs = inMbrConfigMapper.selectByExample(inMbrConfigExample);
        if (inMbrConfigs == null && inMbrConfigs.isEmpty()) {
            mbrScoreNoticeDTO.setGiftScore(new BigDecimal(0));
        }else {
            mbrScoreNoticeDTO.setGiftScore(inMbrConfigs.get(0).getGiftScore());
        }
        return mbrScoreNoticeDTO;
    }

}
